import os
from pydub import AudioSegment

def convert_to_mp3(input_file, output_file):
    """将音频文件转换为MP3格式"""
    try:
        audio = AudioSegment.from_file(input_file)
        audio.export(output_file, format="mp3", bitrate="192k")
        print(f"转换成功: {input_file} -> {output_file}")
    except Exception as e:
        print(f"转换失败: {input_file} - 错误: {e}")

def main():
    # 获取当前文件夹
    current_dir = os.getcwd()
    
    # 支持的输入格式
    supported_formats = ('.ogg', '.wav')
    
    # 遍历当前文件夹
    for file in os.listdir(current_dir):
        if file.lower().endswith(supported_formats):
            input_path = os.path.join(current_dir, file)
            output_path = os.path.splitext(input_path)[0] + ".mp3"
            
            # 如果输出文件已存在，跳过
            if os.path.exists(output_path):
                print(f"文件已存在，跳过: {output_path}")
                continue
            
            convert_to_mp3(input_path, output_path)

if __name__ == "__main__":
    main()